package com.yanggeng.demo.beginnerguidedemo.activity;

import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.arialyy.annotations.Download;
import com.arialyy.aria.core.Aria;
import com.arialyy.aria.core.download.DownloadEntity;
import com.arialyy.aria.core.download.DownloadTask;
import com.arialyy.aria.util.CommonUtil;
import com.bumptech.glide.Glide;
import com.yanggeng.demo.beginnerguidedemo.R;
import com.yanggeng.demo.beginnerguidedemo.utils.ToastUtil;
import com.yanzhenjie.album.Action;
import com.yanzhenjie.album.Album;
import com.yanzhenjie.album.AlbumConfig;
import com.yanzhenjie.album.AlbumFile;
import com.yanzhenjie.album.AlbumLoader;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class FTPTestActivity extends BaseActivity {

    private static final String TAG = "FTPTestActivity";

    private final String USERNAME = "";
    private final String PASSWORD = "";
    private final String FILE_PATH = "";

    private List<String> stringList = new ArrayList<>();

    @BindView(R.id.et_download)
    EditText etDownload;
    @BindView(R.id.btn_download)
    Button btnDownload;
    @BindView(R.id.et_upload)
    EditText etUpload;
    @BindView(R.id.btn_select_file)
    Button btnSelectFile;
    @BindView(R.id.btn_upload)
    Button btnUpload;
    @BindView(R.id.et_username)
    EditText etUsername;
    @BindView(R.id.et_password)
    EditText etPassword;

    private String mImagePath;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ftp_test);
        ButterKnife.bind(this);
        setTitle(true, "ftp test");
        Aria.download(this).register();
        Album.initialize(AlbumConfig.newBuilder(this)
                .setAlbumLoader(new AlbumLoader() {
                    @Override
                    public void load(ImageView imageView, AlbumFile albumFile) {
                        load(imageView, albumFile.getPath());
                    }

                    @Override
                    public void load(ImageView imageView, String url) {
                        Glide.with(imageView.getContext())
                                .load(url)
                                .into(imageView);
                    }
                }).build());

//        stringList.stream().map(w -> w.toUpperCase());
    }

    @OnClick({R.id.btn_download, R.id.btn_select_file, R.id.btn_upload})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_download:
                download();
                break;
            case R.id.btn_select_file:
                Album.image(this)
                        .singleChoice()
                        .camera(false)
                        .onResult(new Action<ArrayList<AlbumFile>>() {
                            @Override
                            public void onAction(@NonNull ArrayList<AlbumFile> result) {
                                if (result.size() > 0) {
                                    mImagePath = result.get(0).getPath();
                                }
                            }
                        })
                        .onCancel(new Action<String>() {
                            @Override
                            public void onAction(@NonNull String result) {
                                Log.e("ImagePointActivity", result);
                            }
                        })
                        .start();
                break;
            case R.id.btn_upload:
                upload();
                break;
        }
    }

    private void download() {
        etDownload.setText("ftp://192.168.137.1:21/img");
        etUsername.setText("ftptest1");
        etPassword.setText("123123");
        String url = etDownload.getText().toString().trim();
        if (TextUtils.isEmpty(url)) {
            ToastUtil.centerToast(this, "请输入下载地址");
            return;
        }
        String username = etUsername.getText().toString().trim();
        if (TextUtils.isEmpty(username)) {
            ToastUtil.centerToast(this, "请输入用户名");
            return;
        }
        String password = etPassword.getText().toString().trim();
        if (TextUtils.isEmpty(password)) {
            ToastUtil.centerToast(this, "请输入密码");
            return;
        }
        String filePath = "/mnt/sdcard/ftptest/";
        File file = new File(filePath);
        if (!file.exists()) {
            file.mkdir();
        }
//        DownloadEntity downloadEntity = Aria.download(this).getDownloadEntity(url);
        Aria.download(this)
                .loadFtpDir(url)
                .charSet("UTF-8")
                .setDirPath(filePath)
                .login(username, password)
                .start();
    }

    private void upload() {
        if (TextUtils.isEmpty(mImagePath)) {
            ToastUtil.centerToast(context, "请选择图片");
            return;
        }

        File file = new File(mImagePath);

        Aria.upload(this)
                .loadFtp(mImagePath)
                .charSet("UTF-8")
                .setUploadUrl("ftp://192.168.137.1:21/img")
                .login("ftptest1", "123123")
                .start();
    }

    @Download.onPre()
    protected void onPre(DownloadTask task) {
        Log.d(TAG, "ftp pre");
    }

    @Download.onTaskPre()
    protected void onTaskPre(DownloadTask task) {
        Log.d(TAG, "ftp task pre");
    }

    @Download.onTaskStart()
    void taskStart(DownloadTask task) {
        Log.d(TAG, "ftp task start");
    }

    @Download.onTaskRunning()
    protected void running(DownloadTask task) {

    }

    @Download.onTaskResume()
    void taskResume(DownloadTask task) {
        Log.d(TAG, "ftp task resume");
    }

    @Download.onTaskStop()
    void taskStop(DownloadTask task) {
        Log.d(TAG, "ftp task stop");
    }

    @Download.onTaskCancel()
    void taskCancel(DownloadTask task) {

    }

    @Download.onTaskFail()
    void taskFail(DownloadTask task) {
        Log.d(TAG, "ftp task fail");
    }

    @Download.onTaskComplete()
    void taskComplete(DownloadTask task) {
        Log.d(TAG, "md5 ==> " + CommonUtil.getFileMD5(new File(task.getDownloadPath())));
        ToastUtil.centerToast(this, "文件：" + task.getEntity().getFileName() + "，下载完成");
    }

}
